
package org.sopt.dev.ImgKnight.monitor.gui.builder;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.sopt.dev.ImgKnight.monitor.Monitor;
import org.sopt.dev.ImgKnight.monitor.dialog.AboutDialog;


public class MenuBuilder implements Builder {
	
	private Monitor monitor = null;
	
	private Menu bar = null;
	
	private MenuItem freqHighItem = null;
	
	private MenuItem freqNormalItem = null;
	
	private MenuItem freqLowItem = null;
	
	public static final int FREQ_HIGH = 0;
	
	public static final int FREQ_NORMAL = 1;
	
	public static final int FREQ_LOW = 2;
	
	public MenuBuilder(Monitor monitor, Menu bar) {
		this.monitor = monitor;
		this.bar = bar;
	}
	
	@Override
	public void build() {
		createFileMenu(bar);
		createOptionMenu(bar);
		createViewMenu(bar);
		createHelpMenu(bar);
	}
	
	private void createFileMenu(Menu parent) {
		Menu menu = new Menu(parent);
		MenuItem header = new MenuItem(parent, SWT.CASCADE);
		header.setText(Monitor.getResourceString("menu.File.text"));
		header.setMenu(menu);	

		MenuItem exitItem = new MenuItem(menu, SWT.PUSH);
		exitItem.setText(Monitor.getResourceString("menu.File.Exit.text"));
		exitItem.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				monitor.getShell().close();
			}
		});
	}
	
	private void createOptionMenu(Menu parent) {
		Menu menu = new Menu(parent);
		MenuItem header = new MenuItem(parent, SWT.CASCADE);
		header.setText(Monitor.getResourceString("menu.Option.text"));
		header.setMenu(menu);
		final MenuItem trayItem = new MenuItem(menu, SWT.CHECK);
		trayItem.setText(Monitor.getResourceString("menu.Option.Tray.text"));
		trayItem.setSelection(false);
		trayItem.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				monitor.setMinToTray(trayItem.getSelection());
			}
		});
	}
	
	private void createViewMenu(Menu parent) {
		Menu menu = new Menu(parent);
		MenuItem header = new MenuItem(parent, SWT.CASCADE);
		header.setText(Monitor.getResourceString("menu.View.text"));
		header.setMenu(menu);
		freqHighItem = new MenuItem(menu, SWT.RADIO);
		freqHighItem.setText(Monitor.getResourceString("menu.View.Frequency.High.text"));
		freqHighItem.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				monitor.setFrequency(FREQ_HIGH);
			}
		});
		
		freqNormalItem = new MenuItem(menu, SWT.RADIO);
		freqNormalItem.setText(Monitor.getResourceString("menu.View.Frequency.Normal.text"));
		freqNormalItem.setSelection(true);
		freqNormalItem.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				monitor.setFrequency(FREQ_NORMAL);
			}
		});
		
		freqLowItem = new MenuItem(menu, SWT.RADIO);
		freqLowItem.setText(Monitor.getResourceString("menu.View.Frequency.Low.text"));
		freqLowItem.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				monitor.setFrequency(FREQ_LOW);
			}
		});
		
		@SuppressWarnings("unused")
		MenuItem separator = new MenuItem(menu, SWT.SEPARATOR);
		MenuItem refreshItem = new MenuItem(menu, SWT.PUSH);
		refreshItem.setText(Monitor.getResourceString("menu.View.Refresh.text"));
		refreshItem.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				/* TODO: */
			}
		});
	}
	
	private void createHelpMenu(Menu parent) {
		Menu menu = new Menu(parent);
		MenuItem header = new MenuItem(parent, SWT.CASCADE);
		header.setText(Monitor.getResourceString("menu.Help.text"));
		header.setMenu(menu);
		
		MenuItem helpItem = new MenuItem(menu, SWT.PUSH);
		helpItem.setText(Monitor.getResourceString("menu.Help.Help.text"));
		helpItem.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				/* TODO: */
			}
		});
		
		@SuppressWarnings("unused")
		MenuItem separator = new MenuItem(menu, SWT.SEPARATOR);
		MenuItem aboutItem = new MenuItem(menu, SWT.PUSH);
		aboutItem.setText(Monitor.getResourceString("menu.Help.About.text"));		
		aboutItem.addSelectionListener(new SelectionAdapter () {
			public void widgetSelected(SelectionEvent e) {
				AboutDialog dialog = new AboutDialog(monitor.getDisplay(), monitor.getShell());
				dialog.open();
			}
		});
	}
	
	public void setSelectedFreq(int freq) {
		if( freq == FREQ_HIGH ) {
			freqHighItem.setSelection(true);
			freqNormalItem.setSelection(false);
			freqLowItem.setSelection(false);
		}
		else if( freq == FREQ_NORMAL ) {
			freqHighItem.setSelection(false);
			freqNormalItem.setSelection(true);
			freqLowItem.setSelection(false);
		}
		else if( freq == FREQ_LOW ) {
			freqHighItem.setSelection(false);
			freqNormalItem.setSelection(false);
			freqLowItem.setSelection(true);
		}
	}
}
